//
//  XMPPTool.m
//  WeChat
//
//  Created by wangcarl on 16/8/29.
//  Copyright © 2016年 wangcarl. All rights reserved.
//

#import "XMPPTool.h"
#import "XMPPFramework.h"
#import "Account.h"

@interface XMPPTool ()<XMPPStreamDelegate>
{
    
    XMPPResultBlock _resultBlock;
    
    
    
}
-(void)setupSteam;
-(void)connectToHost;
-(void)sendPWDToHost;
-(void)sendOnline;
-(void)sendOffline;
-(void)disconnectFromHost;
@end


@implementation XMPPTool
singleton_implementation(XMPPTool);

#pragma mark -public method
-(void)xmppLogin:(XMPPResultBlock)resultBlock
{
    [_xmppStream disconnect];
    _resultBlock=resultBlock;
    
    
    [self connectToHost];
}

-(void)xmppRegister:(XMPPResultBlock)resultBlock
{
    [_xmppStream disconnect];
    
    _resultBlock=resultBlock;
    [self connectToHost];
}


-(void)xmppStreamDidRegister:(XMPPStream *)sender
{
    NSLog(@"%s",__func__);
    if (_resultBlock) {
        _resultBlock(XMPPResultTypeRegisterSucess);
    }

}
-(void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error
{
    NSLog(@"%@",error);
    if (_resultBlock) {
        _resultBlock(XMPPResultTypeRegisterFailure);
    }
}
#pragma mark -private method
-(void)setupSteam
{
    _xmppStream=[[XMPPStream alloc] init];
    _vCardStorage=[XMPPvCardCoreDataStorage sharedInstance];
    _vCard=[[XMPPvCardTempModule alloc] initWithvCardStorage:_vCardStorage];
    [_vCard activate:_xmppStream];
    _avatar=[[XMPPvCardAvatarModule alloc] initWithvCardTempModule:_vCard];
    [_avatar activate:_xmppStream];
    
    [_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)];
    _rosterStorage=[[XMPPRosterCoreDataStorage alloc] init];
    
    _roster=[[XMPPRoster alloc] initWithRosterStorage:_rosterStorage ];
    [_roster activate:_xmppStream];
    
    _msgArchivingStorage=[[XMPPMessageArchivingCoreDataStorage alloc] init];
    _msgArchiving=[[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:_msgArchivingStorage];
    [_msgArchiving  activate:_xmppStream];
    
    
    [_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)];

}
-(void)connectToHost

{
    
    if (!_xmppStream) {
        [self setupSteam];
    }
    XMPPJID *myJid=nil;
    Account *account= [Account shareAccount];
    if (self.isRegister) {
        NSString *user=account.registerUser;
        myJid=[XMPPJID jidWithUser:user domain:account.domain  resource:nil];
        
    }else
    {
        NSString *user=[Account shareAccount].user;
        myJid=[XMPPJID jidWithUser:user domain:account.domain resource:nil];
    
    }
    
    _xmppStream.myJID=myJid;
    _xmppStream.hostName=account.host;
    _xmppStream.hostPort=account.port;
    NSError *error=nil;
    [_xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error];
    if (error) {
        NSLog(@"%@",error);
    }else
    {
        NSLog(@"dsfaf");
    }
}

-(void)sendPWDToHost
{
    NSString *pwd=[Account shareAccount].pwd;
    NSError *error=nil;
    [_xmppStream authenticateWithPassword:pwd error:&error];
    if (error) {
        NSLog(@"%@",error);
    }
}

#pragma mark    -xmppstream delegate

-(void)xmppStreamDidConnect:(XMPPStream *)sender
{
    NSLog(@"%s",__func__);
    
    if (self.isRegister) {
        NSError *error=nil;
        NSString *registerPwd=[Account shareAccount].registerPwd;
        [_xmppStream registerWithPassword:registerPwd error:&error];
        if (error) {
            NSLog(@"%@",error);
        }
    }
    
    
    [self sendPWDToHost];
}
-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
    Log(@"登录成功");
//    NSLog(@"%s",__func__);
    [self sendOnline];
    if (_resultBlock) {
        _resultBlock(XMPPResultTypeLoginSucess);
    }
}

-(void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error
{
    NSLog(@"%s",__func__);
    if (_resultBlock) {
        _resultBlock(XMPPResultTypeLoginFailure);
    }
    
    
}

-(void)sendOnline
{
    XMPPPresence *presence=[XMPPPresence presence];
    NSLog(@"%@",presence);
    [_xmppStream sendElement:presence];
}
-(void)disconnectFromHost
{
    [_xmppStream disconnect];
}
-(void)sendOffline
{
    XMPPPresence *offline=[XMPPPresence presenceWithType:@"unavailable"];
    [_xmppStream sendElement:offline];
    
}
-(void)xmppLogout;
{
    [self sendOffline];
    [self disconnectFromHost];
}
-(void)teardownStream
{
    [_xmppStream removeDelegate:self];
    [_avatar deactivate];
    [_xmppStream disconnect];
    [_roster deactivate];
    [_msgArchiving deactivate];
    _vCardStorage=nil;
    _vCard=nil;
    _avatar=nil;
    _xmppStream=nil;
    _roster=nil;
    _rosterStorage=nil;
    _msgArchiving=nil;
    _msgArchivingStorage=nil;
}
-(void)dealloc
{
    [self teardownStream];
}
@end
